<?php

/**
 * news模型
 * @author 
 * @version 1.0
 * @package application
 * @subpackage application/models
 */
class News_Model extends MY_Model {

    public function __construct() {
        parent::__construct();
        $this->_pk = 'id';
        $this->_attributes = array(
            'id' => '',
            'parentid' => '',
            'nodepath' => '',
            'title' => '',
            'content' => '',
            'keywords' => '',
            'description' => '',
            'imagemark' => '',
            'big_img' => '',
            'author' => '',
            'iscommend' => '',
            'depth' => '',
            'clickcount' => '',
            'joinip' => '',
            'article_source' => '',
            'recycled' => '',
            'created' => '',
            'modified' => '',
            'status' => '',
        );
    }

    public function get_by_parentid($parentid, $limit = 6) {//根据父类id获取指定条数的信息
        $query = $this->db->select($this->get_datas_str())->from($this->table_name())
                        ->where('parentid', $parentid)->limit($limit)->order_by('created', 'desc');

        return $query->get()->result_array();
    }

    public function get_by_pids($parentid, $limit, $offset) {//根据父类id获取分页数据
        $this->db->select($this->get_datas_str())->from($this->table_name())->where('parentid', $parentid);
        $query = $this->db->limit($limit, $offset)->get();
        return $query->result_array(); //数组形式
    }

    public function get_value_by_pid($pid) {//根据文章类别的父类id获取文章指定id
        $query = $this->db->select($id)->from($this->table_name())
                ->where('parentid', $pid);
        $arr = $query->get()->row_array();
        if ($arr) {//只取数组id字段
            return $arr['id'];
        }
    }

    public function get_by_pid_id($pid, $id) {//根据父id和文章id获取文章内容
        $query = $this->db->select($this->get_datas_str())->from($this->table_name())
                ->where(array('parentid' => $pid, 'id' => $id));
        return $query->get()->row_array();
    }

    //上一篇
    public function pre_news($pid, $id) {
        $query = $this->db->select($this->get_datas_str())->from($this->table_name())
                        ->where(array('parentid' => $pid, 'id <' => $id))//id与<之间要有空格
                        ->order_by("id", "desc")->limit(1, 0);
        return $query->get()->row_array();
    }

    //下一篇
    public function next_news($pid, $id) {
        $query = $this->db->select($this->get_datas_str())->from($this->table_name())
                        ->where(array('parentid' => $pid, 'id >' => $id))//id与>之间要有空格
                        ->order_by("id", "asc")->limit(1, 0);
        return $query->get()->row_array();
    }

}
